package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.password.MyMD5;
import com.server.service.ICommonDataService;
import com.server.service.IMngUserService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller("MngUserController")
@RequestMapping("adminUser")
public class MngUserController {
	
	private String tableName = "education.admin_user";
	
	@Autowired
	private IMngUserService mngUserService;
	@Autowired
	private ICommonDataService commonDataService;
	
	@Login
	@RequestMapping("/{type}")
	public ModelAndView userList(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		ModelAndView mv = new ModelAndView("mngUser/userList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("type", type);
		return mv;
	}
	@Login
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response) {
		String _whereStr = "valid=1 and type=1";
		String sortStr = "createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@Login
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		Map<String, Object> formData = commonDataService.getFormData(tableName, "id", id, null, null);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@Login
	@RequestMapping("check/{id}/{state}/{type}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/mngUser/userForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("state", state);
		mv.addObject("type", type);
		mv.addObject("business", "adminUser");
		mv.addObject("titleName", "查看管理员");
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("add/{type}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/mngUser/userForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("type",type);
		mv.addObject("business", "adminUser");
		mv.addObject("titleName", "新增管理员");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("save/{id}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String formData = request.getParameter("formData");
		Map<String, Object> saveMap = JSONSerielizer.toMap(formData);
		UUID uuid = UUID.randomUUID();
		Boolean isSuccess = true;
		String info = null;
		if (id.equals(0)) {
			String nickname  = saveMap.get("nickname").toString(); 
			isSuccess = mngUserService.check(nickname);
			if(isSuccess){
				saveMap.put("uuid", uuid);
				saveMap.put("type", 1);
				if(!StringUtils.isEmpty(saveMap.get("password").toString())){
					saveMap.put("password", MyMD5.circularlyEncrypt(saveMap.get("password").toString(),3));
				}else{
					saveMap.put("password", MyMD5.circularlyEncrypt("123456",3));
				}
				isSuccess = commonDataService.add(operator, tableName, saveMap);
			}else{
				info = "账号已存在";
			}
			
		}else {
			if(!StringUtils.isEmpty(saveMap.get("password").toString())){
				saveMap.put("password", MyMD5.circularlyEncrypt(saveMap.get("password").toString(),3));
			}
			isSuccess = commonDataService.update(operator, tableName, "id", id.toString(), saveMap);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		map.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@Login
	@RequestMapping("update/{id}/{state}/{type}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/mngUser/userForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("type", type);
		mv.addObject("state", state);
		mv.addObject("business", "adminUser");
		mv.addObject("titleName", "编辑管理员信息");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Integer estimate = mngUserService.deleteAdminUser(ids);
		OutputUtil.returnString(response, estimate.toString());
	}
	@RequestMapping("roleList")
	public void roleList(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = mngUserService.roleList();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	@RequestMapping("getOrg")
	public void getOrg(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = mngUserService.getOrg();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	@RequestMapping("getField")
	public void getField(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = mngUserService.getField();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
}
